import Toast from '../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
export function initLoadMoreMixin() {
    console.log('list-load',this)
    let methods = {
        'onReachBottom': this.onReachBottom
    }
    this.setData({
        pages: {
            perPage: 10,
            currPage: 1,
            pageCount: 1
        },
        items: [],
        loading: false,
        finished: false,
        isEmpty: false
    })

    this.resetInit = async () => {
        console.log('this.resetData,,,',this)
        this.resetData();
		
        const page = await this.initData();
        setTimeout(() => {
            this.setPages(page);
        },500)
    }

    this.isFinished = () => {
        this.setData({
            finished: true,
            loading: false
        })
    }

    this.onReachBottom= () => {

        this.loadMore()
        //
        if (methods.onReachBottom) {
            methods.onReachBottom()
        }
    }

    this.loadMore = async () => {
        console.log("loadmore:" + this.data.loading);
        const vm = this;
        if (vm.data.pages.pageCount < vm.data.pages.perPage) {
            Toast({
                message: "没有更多了~"
            });
            vm.isFinished();
        }else{
            vm.setData({loading: true})
           
            const page = await vm.initData(true);
    
            vm.nextPage(page.pageCount);
    }}

    this.nextPage = (pageCount = 1) => {
        this.setData({
            [`pages.currPage`]: this.data.pages.currPage + 1,
            [`pages.pageCount`]: pageCount,
            loading: false
        })
    }
    
   this.setPages = (page = {}) =>{
    this.setData({isEmpty: page.totalCount === 0})
   
    if(page.totalCount <= this.data.pages.perPage){
        //不满一页
        this.isFinished();
    }else{
        //下一页
        this.nextPage(page.pageCount);
    }
   }

    this.resetData = () =>{
        this.setData({
            items: [],
            pages: {
                perPage: 10,
                currPage: 1,
                pageCount: 1
            },
            loading: true,
            finished: false,
            isEmpty:false
        })
    }
}
